/*
 * Copyright (c) 2020 hebeiyiliuba.com
 * All rights reserved.
 *
 */
package org.jeecg.modules.third.aipocr;

import com.baidu.aip.face.AipFace;
import com.baidu.aip.ocr.AipOcr;
import org.jeecg.common.constant.AIConstant;

import javax.annotation.Resource;

/**
 * 构建AipOcr工厂模式
 *
 * @author duanlikao
 * @created 2020/11/28
 */
public class AipFactory {

    @Resource
    private static AipOcr aipOcr;

    @Resource
    private static AipFace aipFace;

    private static final int CONNECTION_TIME_OUT =  2000;

    private static final int SOCKET_TIME_OUT =  60000;

    public  AipOcr getAipOcr() {
        if (aipOcr == null) {
            synchronized (AipOcr.class) {
                if (aipOcr == null) {
                    aipOcr = new AipOcr(
                            AIConstant.AI_APPID,
                            AIConstant.AI_API_KEY,
                            AIConstant.AI_SECRET_KEY);
                    // 可选：设置网络连接参数
                    aipOcr.setConnectionTimeoutInMillis(CONNECTION_TIME_OUT);
                    aipOcr.setSocketTimeoutInMillis(SOCKET_TIME_OUT);

                    // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
                    //aipOcr.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
                    //aipOcr.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

                    // 可选：设置log4j日志输出格式，若不设置，则使用默认配置
                    // 也可以直接通过jvm启动参数设置此环境变量
                    //System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");
                }
            }
        }
        return aipOcr;

    }

    public  AipFace getAipFace() {
        if (aipFace == null) {
            synchronized (AipFace.class) {
                if (aipFace == null) {
                    aipFace = new AipFace(
                            AIConstant.AI_APPID,
                            AIConstant.AI_API_KEY,
                            AIConstant.AI_SECRET_KEY);
                    // 可选：设置网络连接参数
                    aipFace.setConnectionTimeoutInMillis(CONNECTION_TIME_OUT);
                    aipFace.setSocketTimeoutInMillis(SOCKET_TIME_OUT);
                }
            }
        }
        return aipFace;
    }


}